<?php

namespace Sffi\ElementPlus\ElRule;

use Sffi\ElementPlus\base\EntityToArray;

class Law
{
    use EntityToArray;

    /**
     * @var bool 是否必填
     */
    protected bool $required = false;
    /**
     * @var string 验证失败提示信息
     */
    protected string $message = '';
    /**
     * @var string 验证类型
     */
    protected string $type = '';
    /**
     * @var string|mixed 验证时机 仅支持两种： blur失焦、change实时
     */
    protected string $trigger = '';
    /**
     * @var string 正则表达式
     */
    protected string $pattern = '';
    /**
     * @var array 选项
     */
    protected array $enum = [];
    /**
     * @var string 验证方法，需配合前端使用
     */

    protected string $validator = '';
    protected int $len;
    protected int $min;
    protected int $max;

    public function __construct($message, $trigger='blur')
    {
        $this->message = $message;
        if (in_array($trigger, ['blur', 'change'])) $this->trigger = $trigger;
    }

    /**
     * @return bool
     */
    public function isRequired(): bool
    {
        return $this->required;
    }

    /**
     * @param bool $required
     */
    public function setRequired(bool $required): self
    {
        $this->required = $required;
        return $this;
    }

    /**
     * @return string
     */
    public function getMessage(): string
    {
        return $this->message;
    }

    /**
     * @param string $message
     */
    public function setMessage(string $message): self
    {
        $this->message = $message;
        return $this;
    }

    /**
     * @return string
     */
    public function getType(): string
    {
        return $this->type;
    }

    /**
     * @param string $type
     */
    public function setType(string $type): self
    {
        $this->type = $type;
        return $this;
    }

    /**
     * @return string
     */
    public function getTrigger(): string
    {
        return $this->trigger;
    }

    /**
     * @param string $trigger
     */
    public function setTrigger(string $trigger): self
    {
        $this->trigger = $trigger;
        return $this;
    }

    /**
     * @return string
     */
    public function getValidator(): string
    {
        return $this->validator;
    }

    /**
     * @param string $validator
     */
    public function setValidator(string $validator): self
    {
        $this->validator = $validator;
        return $this;
    }

    /**
     * @return string
     */
    public function getPattern(): string
    {
        return $this->pattern;
    }

    /**
     * @param string $pattern
     */
    public function setPattern(string $pattern): self
    {
        $this->pattern = $pattern;
        return $this;
    }

    /**
     * @return array
     */
    public function getEnum(): array
    {
        return $this->enum;
    }

    /**
     * @param array $enum
     */
    public function setEnum(array $enum): self
    {
        $this->enum = $enum;
        return $this;
    }

    /**
     * @return int
     */
    public function getLength(): int
    {
        return $this->len;
    }

    /**
     * @param int $length
     */
    public function setLength(int $length): self
    {
        $this->len = $length;
        return $this;
    }

    /**
     * @return int
     */
    public function getMin(): int
    {
        return $this->min;
    }

    /**
     * @param int $min
     */
    public function setMin(int $min): self
    {
        $this->min = $min;
        return $this;
    }

    /**
     * @return int
     */
    public function getMax(): int
    {
        return $this->max;
    }

    /**
     * @param int $mas
     */
    public function setMax(int $max): self
    {
        $this->max = $max;
        return $this;
    }


    const LAW_TYPE_STRING = 'string';
    const LAW_TYPE_NUMBER = 'number';
    const LAW_TYPE_BOOLEAN = 'boolean';
    const LAW_TYPE_METHOD = 'method';
    const LAW_TYPE_REGEXP = 'regexp';
    const LAW_TYPE_INTEGER = 'integer';
    const LAW_TYPE_FLOAT = 'float';
    const LAW_TYPE_ARRAY = 'array';
    const LAW_TYPE_OBJECT = 'object';
    const LAW_TYPE_ENUM = 'enum';
    const LAW_TYPE_DATE = 'date';
    const LAW_TYPE_URL = 'url';
    const LAW_TYPE_HEX = 'hex';
    const LAW_TYPE_EMAIL = 'email';
}